<?php
namespace Admin\Controller;

class ManJianController extends PublicController {
    function _initialize() {
        parent::_initialize ();
    }

    public function index() {
        $store_id = session("wadmin");
        if ($store_id['store_id']==0) {
            $map = '';
        } else {
            $map['p.store_id'] = $store_id['store_id'];
        }

        $count = M("PromotionManjian")->alias("p")->where($map)->count (); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 12);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");//(对thinkphp自带分页的格式进行自定义)
        $show = $Page->show (); // 分页显示输出
        $result = M("PromotionManjian")->alias("p")->join("meal_store as s on p.store_id=s.store_id")->field("p.*,s.store_name")->where($map)->limit ( $Page->firstRow . ',' . $Page->listRows )->order("p.id desc")->select ();

        //店铺信息（所有的店铺）
        $store = M('store')->field('store_id,store_name')->select();

        $this->assign ( "store", $store );
        $this->assign ( "result", $result );
        $this->assign ( "page", $show ); // 赋值分页输出
        $this->display ();
    }

    public function edit()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id']==0) {
            $storeId = 0;
        } else {
            $storeId = $store_id['store_id'];
        }

        $id = I("get.id");
        $result = M("PromotionManjian")->where(array("id" => $id))->field('id,name,price,start_time,end_time,condition_price,store_id')->find();

        //将时间戳转换成日期格式
        $result['start_time'] = date("Y-m-d ", $result['start_time']);
        $result['end_time'] = date("Y-m-d ", $result['end_time']);

        //店铺信息（所有的店铺）
        $store = M('store')->field('store_id,store_name')->select();

        $this->assign ( "storeId", $storeId ); //店铺id(登录的)
        $this->assign ( "store", $store );
        $this->assign ( "result", $result );
        $this->display ();
    }

    public function add()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id']==0) {
            $storeId = 0;
        } else {
            $storeId = $store_id['store_id'];
        }

        //店铺信息（所有的店铺）
        $store = M('store')->field('store_id,store_name')->select();

        $this->assign ( "storeId", $storeId );
        $this->assign ( "store", $store );
        $this->display ();
    }

    public function addSave()
    {
        $data =I("post.");
        if($data && is_array($data)) {
            if(preg_match("/^[1-9]\d*(\.\d+)?$/",$data['price'] ) == 0){
                $this->error("请输入正确的满减价格");
            }

            if(preg_match("/^[1-9]\d*(\.\d+)?$/",$data['condition_price'] ) == 0){
                $this->error("请输入正确的条件价格");
            }
            $data['id'] = time().randNum(0,5);

            $store_id = session("wadmin");
            if ($store_id) {
                $data['creater'] = $store_id['user_name']; //登录管理员
            } else {
                $data['creater'] = "";
            }

            if(!empty($data['start_time'])){
                $data['start_time'] = strtotime($data['start_time']);
            }else{
                $this->error("请输入正确的开始时间");
            }
            if(!empty($data['end_time'])){
                $data['end_time'] = strtotime($data['end_time']);
            }else{
                $this->error("请输入正确的结束时间");
            }

            if ($store_id['store_id']==0) {
                $data['store_id'] = $data['store_id']; // 店铺ID
            } else {
                $data['store_id'] = $store_id['store_id']; // 店铺ID
            }

            $data['modify_time'] = time();
            $data['create_time'] = time();
            $result = M('PromotionManjian')->add($data);
            if($result !== false){
                $this->success("新增满减成功！",U('Admin/ManJian/index'));
            }else{
                $this->error("操作失败");
            }
        }else{
            $this->error("操作失败");
        }
    }

    public function del()
    {
        $id = I("get.id");
        $result = M("PromotionManjian")->where(array(
            "id" => $id
        ))->delete();
        if ($result !== false) {
            $this->success("删除满减成功！");
        }else{
            $this->error("操作失败");
        }
    }

    public function save()
    {
        $data = I("post.");

        if($data && is_array($data)){
            if(preg_match("/^[1-9]\d*(\.\d+)?$/",$data['price'] ) == 0){
                $this->error("请输入正确的满减价格");
            }

            if(preg_match("/^[1-9]\d*(\.\d+)?$/",$data['condition_price'] ) == 0){
                $this->error("请输入正确的条件价格");
            }

            $id = $data['id'];
            //查找是否存在
            $manJian = M("PromotionManjian")->where(array("id" => $id))->find();

            $store_id = session("wadmin");
            if ($store_id) {
                $data['modifier'] = $store_id['user_name']; //登录管理员
            } else {
                $data['modifier'] = "";
            }

            $data['modify_time'] = time();

            if(!empty($data['start_time'])){
                $data['start_time'] = strtotime($data['start_time']);
            }else{
                $this->error("请输入正确的开始时间");
            }
            if(!empty($data['end_time'])){
                $data['end_time'] = strtotime($data['end_time']);
            }else{
                $this->error("请输入正确的结束时间");
            }

            if($manJian){
                $result = M("PromotionManjian")->where("id='$id'")->save($data);
                if($result !== false){
                    $this->success("修改满减成功！",U('Admin/ManJian/index'));
                }else{
                    $this->error("操作失败");
                }
            }else{
                $this->error("满减不存在");
            }
        }else{
            $this->error("操作失败");
        }
    }

}